﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DomusDotNet.Sbm.Client.SL4.ViewModels;
using Moq;
using SharpTestsEx;
using DomusDotNet.Sbm.Client.SL4.Services;
using System.Security.Principal;
using Caliburn.Micro;

namespace DomusDotNet.Sbm.Client.SL4.Tests
{
    [TestClass]
    public class the_ShellViewModel : TestBase
    {
        Mock<ISecurityContext> securityCtx;
        Mock<IEventAggregator> eventAggregator;
        ShellViewModel sut;
        [TestInitialize]
        public void before_each()
        {
            eventAggregator = new Mock<IEventAggregator>();
            securityCtx = new Mock<ISecurityContext>();


        }

        void CreateAndActivateShell()
        {
            var loginVM = new LoginViewModel(eventAggregator.Object);
			var searchVM = new SearchViewModel(null, null, null);

            sut = new ShellViewModel(securityCtx.Object, eventAggregator.Object,
                () => loginVM, () => searchVM
            );

			((IActivate)sut).Activate();
        }

        [TestMethod]
        public void should_show_Login_when_not_authenticated()
        {
            securityCtx.SetupGet(x => x.CurrentUser).Returns(DefaultPrincipal.Null);
            CreateAndActivateShell();


            sut.ActiveItem.Should().Be.OfType<LoginViewModel>();
        }

        [TestMethod]
        public void should_show_Search_when_already_authenticated()
        {
            securityCtx.SetupGet(x => x.CurrentUser).Returns(new DefaultPrincipal("dummy", Guid.Empty));
            CreateAndActivateShell();


            sut.ActiveItem.Should().Be.OfType<SearchViewModel>();
        }

        [TestMethod]
        public void should_show_Login_after_logging_out()
        {
            securityCtx.SetupGet(x => x.CurrentUser).Returns(new DefaultPrincipal("dummy", Guid.Empty));
            CreateAndActivateShell();

            ((IHandle<LoggedOutEvent>)sut).Handle(new LoggedOutEvent());

            sut.ActiveItem.Should().Be.OfType<LoginViewModel>();
        }


        [TestMethod]
        public void should_show_Search_after_logging_in()
        {
            securityCtx.SetupGet(x => x.CurrentUser).Returns(DefaultPrincipal.Null);
            CreateAndActivateShell();

            ((IHandle<LoggedInEvent>)sut).Handle(new LoggedInEvent { Principal = new DefaultPrincipal("dummy", Guid.Empty) });

            sut.ActiveItem.Should().Be.OfType<SearchViewModel>();
        }


        [TestMethod]
        [Ignore]
        public void should_log_out()
        {

        }



    }

}
